package cn.supermaster.management.core.manager;

import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import cn.supermaster.management.common.utils.ThreadUtils;
import cn.supermaster.management.common.utils.spring.SpringUtils;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.core.manager
 * @ClassName: AsynchronousManager
 * @Description: [异步任务管理器]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/10 22:24
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
public class AsynchronousManager {
    /**
     * 操作延迟10毫秒
     **/
    private final int OPERATE_DELAY_TIME = 10;

    /**
     * 异步操作任务调度线程池
     **/
    private ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");

    /**
     * 单例模式
     */
    private AsynchronousManager() {
    }

    private static AsynchronousManager manager = new AsynchronousManager();

    public static AsynchronousManager manager() {
        return manager;
    }



    /**
     * @Description: [执行任务]
     * <p></P>
     * @Title: shutdown
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param task 任务
     * @Return: void
     */
    public void execute(TimerTask task) {
        executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
    }

    /**
     * @Description: [停止任务线程池]
     * <p></P>
     * @Title: shutdown
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Return: void
     */
    public void shutdown() {
        ThreadUtils.shutdownAndAwaitTermination(executor);
    }
}
